﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using ETL.Common;
using ETL.Common.Logging;
using ETL.Entity;
namespace ETL.DataAccess
{
    public class ProjectTaskRepository
    {
        public IEnumerable<TASK> GetTasksOfProject(decimal projectId)
        {
            if (projectId > 0)
            {
                var db = new ETLDataEntities();
                var result = from s in db.TASKS 
                             where s.GEN_PROJECT_ID == projectId  
                             orderby s.GEN_TASK_ID
                             select s;

                return result;
            }
            else return null;
        }

        public IEnumerable<TASK> GetTasksOfProject(decimal projectId, string userId)
        {
            if (projectId > 0)
            {
                var db = new ETLDataEntities();
                var result = from t in db.TASKS
                             join s in db.SCHEDULED_RESOURCE_USAGES on t.GEN_TASK_ID equals s.GEN_TASK_ID
                             join r in db.RESOURCE_INSTANCES on s.GEN_RESOURCE_ID equals r.GEN_RESOURCE_ID
                             join p in db.PROJECTS on t.GEN_PROJECT_ID equals p.PROJECT_ID
                             where r.RESOURCE_NAME.ToUpper().Equals(userId.ToUpper())
                             && p.PROJECT_ID == projectId
                             orderby t.GEN_TASK_ID
                             select t;

                return result;
            }
            else return null;
        }

        public TASK GetTask(decimal projectId, decimal taskId)
        {
            TASK result = null;
            if (taskId > 0)
            {
                var db = new ETLDataEntities();
                var allTasks = from s in db.TASKS
                             where s.GEN_TASK_ID == taskId && s.GEN_PROJECT_ID == projectId
                             select s;
                if (allTasks.Count() > 0)
                {
                    return allTasks.First();
                }
            }
            return result;
        }

        public TASK GetTaskByTaskId(decimal taskId)
        {
            TASK result = null;
            if (taskId > 0)
            {
                var db = new ETLDataEntities();
                var allTasks = from s in db.TASKS
                               where s.TASK_ID == taskId
                               select s;
                if (allTasks.Count() > 0)
                {
                    return allTasks.First();
                }
            }
            return result;
        }

        public TASK GetTaskByGenTaskId(decimal genTaskId)
        {
            TASK result = null;
            if (genTaskId > 0)
            {
                var db = new ETLDataEntities();
                var allTasks = from s in db.TASKS
                               where s.GEN_TASK_ID == genTaskId
                               select s;
                if (allTasks.Count() > 0)
                {
                    return allTasks.First();
                }
            }
            return result;
        }
        
        /// <summary>
        /// Add new project task and return gen_task_id
        /// </summary>
        /// <param name="taskName"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public int AddNewProjectTask(string taskName, string projectId)
        {
            try
            {
            var db = new ETLDataEntities();
            decimal _pId = decimal.Parse(projectId);
            string projectName = string.Empty;
            PROJECT project = (from p in db.PROJECTS where p.PROJECT_ID == _pId select p).FirstOrDefault();
            if(project != null)
            {
                projectName = project.PROJECT_NAME;
            }
            decimal maxTaskId = 0;
            decimal maxGenTaskId = 0;

            // Select max_taskID
            TASK taskWithMaxTaskId =
                (from p in db.TASKS select p).OrderByDescending(p => p.TASK_ID).FirstOrDefault();

            if (taskWithMaxTaskId != null)
            {
                if (taskWithMaxTaskId.TASK_ID != null)
                    maxTaskId = taskWithMaxTaskId.TASK_ID.Value;
            }

            TASK taskWithMaxGenTaskId =
                 (from p in db.TASKS select p).OrderByDescending(p => p.GEN_TASK_ID).FirstOrDefault();

            if (taskWithMaxGenTaskId != null)
            {
                maxGenTaskId = taskWithMaxGenTaskId.GEN_TASK_ID;
            }
            // Get last task_id
            var task =  new TASK
                        {
                            TASK_ID = maxTaskId + 1,
                            GEN_TASK_ID = maxGenTaskId + 1,
                            TASK_NAME = taskName,
                            GEN_PROJECT_ID = int.Parse(projectId),
                            PROJECT_NAME = projectName
                        };
            db.TASKS.Add(task);
            db.SaveChanges();
            return int.Parse(task.GEN_TASK_ID.ToString());
            return -1;

            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return -1;
            }

        }

        public int UpdateProjectTask(string taskId, string taskName)
        {
            try
            {
                var db = new ETLDataEntities();
                decimal t = Utils.StringToDecimal(taskId);

                TASK currentTask  = (from s in db.TASKS
                                              where s.TASK_ID == t
                                              select s).FirstOrDefault();

                if ( currentTask != null)
                {
                    currentTask.TASK_NAME = taskName;
                    db.Entry(currentTask).State = EntityState.Modified;
                    db.SaveChanges();
                }
                
                return 1;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return -1;
            }
        }

        public int DeleteProjectTask(string taskId)
        {
            try
            {
                var db = new ETLDataEntities();
                decimal t = Utils.StringToDecimal(taskId);
                TASK currentTask = (from s in db.TASKS
                                    where s.TASK_ID == t
                                    select s).FirstOrDefault();

                if (currentTask != null)
                {
                    db.TASKS.Remove(currentTask);
                    db.SaveChanges();
                }

                return 1;
            }
            catch (Exception ex)
            {
                LogService.Service.Error(ex);
                return -1;
            }
        }
    }
}
